package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.WorkRecord;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logRecord.LogRecordReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logRecord.LogRecordResVo;
import com.zmj.sy.mom.srv.aps.mapper.WorkRecordMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.format.DateTimeFormatter;

@RequiredArgsConstructor
@Service
public class LogRecordService {

    private final WorkRecordMapper workRecordMapper;

    @SneakyThrows
    public BasePageResVo<LogRecordResVo> log(LogRecordReqVo reqVo) {

        Page<WorkRecord> page = workRecordMapper.lambdaQuery()
                .eq(reqVo.getType() != null, WorkRecord::getType, reqVo.getType())
                .like(StringUtils.hasText(reqVo.getPlmid()), WorkRecord::getPlmid, reqVo.getPlmid())
                .like(StringUtils.hasText(reqVo.getPalletNumber()),WorkRecord::getPalletNumber,reqVo.getPalletNumber())
                .eq(StringUtils.hasText(reqVo.getPhaseCode()), WorkRecord::getPhaseCode, reqVo.getPhaseCode())
                .eq(reqVo.getWorkType() != null, WorkRecord::getWorkType, reqVo.getWorkType())
                .orderByDesc(WorkRecord::getCreateDateTime)
                .page(reqVo.toPage());


        BasePageResVo<LogRecordResVo> resVo = BasePageResVo.of(page, LogRecordResVo.class);

        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        resVo.getData().forEach(e->{
//            e.setReportTime(e.getWorkTime().format(formatter));
            e.setStartPackageNo(e.getWorkTime().format(formatter));
//            e.setWorkTime(LocalDateTime.parse(e.getWorkTime().format(formatter)));
        });

        return resVo;

//        return BasePageResVo.of(page, LogRecordResVo.class);
    }

}
